package com.niit.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect  // 使用注解定义切面
@Component
public class TimeAspect {

    // @Before @After  @AfterReturning  @AfterThrowing  @Around
    @Before("execution(* add*(..))")
    public void before() {
        System.out.println("before");
    }

    @Around("execution(* add*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        // TODO 相当于 before的通知
        long start = System.currentTimeMillis();
        joinPoint.proceed(); // 执行目标方法
        // TODO 相当于 after的通知
        long end = System.currentTimeMillis();
        System.out.println("耗时: " + (end - start) + "ms");
    }
}
